package com.changgou.search.controller;

import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/sku_search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping
    public Map search(@RequestParam Map<String,String> searchMap){
        //特殊符号处理
        this.handlerSearchMap(searchMap);
        Map search = searchService.search(searchMap);
        return search;
    }

    //特殊符号处理
    private void handlerSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entrySet = searchMap.entrySet();
        for (Map.Entry<String, String> stringEntry : entrySet) {
            //如果是以spec_开头则转换
            if (stringEntry.getKey().startsWith("spec_")){
                searchMap.put(stringEntry.getKey(),stringEntry.getValue().replace("+","%2B"));
            }
        }
    }
}
